package com.cn.intelligence.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbUserMapper;
import com.cn.intelligence.mapper.VipMapper;
import com.cn.intelligence.pojo.EUDataGridResult;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbUser;
import com.cn.intelligence.pojo.Vip;
import com.cn.intelligence.pojo.VipExample;
import com.cn.intelligence.service.VipService;
import com.cn.intelligence.util.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class VipServiceImpl implements VipService {

	@Autowired
	public VipMapper vipMapper;
	@Autowired
	public TbUserMapper tbUserMapper;

	/**
	 * 获取系统用户
	 */
	@Override
	public EUDataGridResult getVipUserList(Integer page, Integer rows,Vip vip) {
		VipExample vipExample = new VipExample();
		VipExample.Criteria criteria= vipExample.createCriteria();
		if(vip.getId()!=null && !"".equals(vip.getId())){
			criteria.andIdEqualTo(vip.getId());
		}
		if(vip.getUid()!=null && !"".equals(vip.getUid())){
			criteria.andUidEqualTo(vip.getUid());
		}
		// 分页查询
		PageHelper.startPage(page, rows);
		List<Vip> vipList = vipMapper.selectByExample(vipExample);
		// 获取总数
		PageInfo<Vip> pageInfo = new PageInfo<Vip>(vipList);
		// 实现返回值设定
		EUDataGridResult euDataGridResult = new EUDataGridResult();
		euDataGridResult.setRows(vipList);
		euDataGridResult.setTotal(pageInfo.getTotal());
		return euDataGridResult;

	}

	/**
	 * 添加修改用户信息
	 */
	@Override
	public TaotaoResult insertVip(Vip vip) {
		
		VipExample vipExample = new VipExample();
		VipExample.Criteria criteria = vipExample.createCriteria();
		int value = 0;
		if(vip.getId() == null || "".equals(vip.getId())){		
			TbUser tbuser = tbUserMapper.selectByPrimaryKey(Long.parseLong(vip.getUname()));
			long ids = IDUtils.genItemId();
			vip.setId(ids);
			vip.setUid(tbuser.getId());
			vip.setUname(tbuser.getNickname());
			vip.getActivetimestart();
			DateTime  dataTime = new DateTime(vip.getActivetimestart());
			dataTime = dataTime.plusDays(vip.getStatusdate());
			vip.setActivetimeend(dataTime.toDate());
			vip.setCreated(new Date());
			vip.setUpdated(new Date());
			value = vipMapper.insert(vip);
		}else{
			criteria.andIdEqualTo(vip.getId());
			Vip exittbMuser = vipMapper.selectByPrimaryKey(vip.getId());	
			if(exittbMuser != null){
				vip.setUpdated(new Date());
				value = vipMapper.updateByExampleSelective(vip, vipExample);	
			}else{
				return TaotaoResult.fail();
			}
		}
		if(value == 0){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}
	
	}

	@Override
	public TaotaoResult deleteUser(Long[] ids) {
		VipExample vipExample = new VipExample();
		VipExample.Criteria criteria = vipExample.createCriteria();
		criteria.andIdIn(Arrays.asList(ids));
		int value = vipMapper.deleteByExample(vipExample);	
		if(value == ids.length){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}
	}

	@Override
	public TaotaoResult cususerbyId(Long id) {

		TbUser tbuser = tbUserMapper.selectByPrimaryKey(id);
		return TaotaoResult.ok(tbuser);
	}
}
